/* --Event tracking--*/
function sendGAEventValue(eventCategory, eventAction, eventLabel, eventValue) {
  try {
    ga("send", "event", eventCategory, eventAction, eventLabel, eventValue);
  } catch (err) {
    console.log("sendGA error " + eventCategory + " | " + eventAction + " | " + eventLabel + " | " + eventValue);
  }
}
function sendGA(eventCategory, eventAction, eventLabel) {
  try {
    ga("send", "event", eventCategory, eventAction, eventLabel);
  } catch (err) {
    console.log("sendGA error " + eventCategory + " | " + eventAction + " | " + eventLabel);
  }
}
function sendGAforSocial(socialNetwork, socialAction, socialTarget) {
  try {
    socialTarget = socialTarget || window.location.pathname;
    ga('send', 'social', socialNetwork, socialAction, socialTarget);
  } catch (e) {
    console.log("social share error " + socialNetwork + " " + socialAction + " " + socialTarget);
  }
}
/**
 * Start
 * Social Shareing Functions
 * FB, Twitter
 */
$("body").on("click", ".indiatvf-icon,.facebook", function () {
  var url = $(this).attr('data-url');
  var title = $(this).attr('data-title');
  sendGAforSocial('Facebook', 'Share', url);
  popUp = window.open("https://www.facebook.com/sharer/sharer.php?u=" + url + "&title=" + title, "popupwindow", "menubar=1,resizable=1,width=600,height=400");
  popUp.focus();
  return false;
});
$("body").on("click", ".indiatvt-icon,.twitter", function () {
  var url = $(this).attr('data-url');
  var title = $(this).attr('data-title');
  sendGAforSocial('Twitter', 'tweet', url);
  var newTitle = (title.length > 240) ? title.substr(0, 235) + '... | via @IndiaTVNews' : title.substr(0, 240)+ ' | via @IndiaTVNews';
  var strUrl = encodeURI("https://twitter.com/share?text=" + newTitle + "&url=" + url);      
  var popUp = window.open(strUrl, "twitterwindow", "menubar=1,resizable=1,width=600,height=400");
  popUp.focus();
  return false;
});
$("body").on("click", ".google-plus", function () {
  var url = $(this).attr('data-url');
  var popUp = window.open("https://plus.google.com/share?url=" + url, 'popupwindow', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');
  popUp.focus();
  return false;
});
$("body").on("click", ".pinteres", function () {
  var title = $(this).attr('data-title');
  var url   = $(this).attr('data-url');
  var image = $(this).attr('data-image');
  var popUp = window.open("https://pinterest.com/pin/create/bookmarklet/?media=" + image + "&url=" + url + "&is_video=false&description=" + title, "popUpWindow", "height=400,width=600,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes");
  popUp.focus();
  return false;
});
$("body").on("click", ".linkedin", function () {
  var title = $(this).attr('data-title');
  var url   = $(this).attr('data-url');
  var popUp = window.open("https://www.linkedin.com/shareArticle?mini=true" + "&url=" + url + "&title=" + title, 'popupwindow', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');
  popUp.focus();
  return false;
});

$(document).ready(function () {
  $("#detailpagead").hide();
  $(".read-more").click(function () {
    sendGA('Story Page', 'Click', 'Read More');
    $(".content").css("height", "auto");
    $(this).hide();
    $("#detailpagead").show();
  });
  
    //MAIN MENU FUNCTION
    var a = 1;
    $(".openMenu").click(function(){
        $('.menu').slideToggle()
        if(a==1){
            $(this).addClass('open');			
            $('body').addClass('schide');
            a=0;
        } else{
            $(this).removeClass('open');
            $('body').removeClass('schide');
            a=1;
        }
    });
    
    //menu search option
    $(".submitSerch").click(function (e) {
      var searchReg = /^[a-zA-Z0-9- '.]+$/;
      if ($(this).attr('id') == "searchbutton") {
        var val = $("#searchfield").val();
        if (val === "") {
          $("#searchfield").attr('class', 'sercfield red');
          $('#searchfield').focus();
          return false;
        }
        if (!searchReg.test(val)) {
          $("#searchfield").attr('class', 'sercfield red');
          alert('Oops! You might have entered the invalid keyword(s). Kindly re-enter.');
          $('#searchfield').focus();
          return false;
        }
      }
      val = $.trim(val);
      str = val.replace(/\s+/g, '-').toLowerCase();
      window.location.href = '/topic/' + str;
      return true;
    });
    $("#searchfield").keypress(function (e) {
      if (e.which == 13) {
        var searchReg = /^[a-zA-Z0-9- '.]+$/;
        var val = $('#searchfield').val();
        if (val === "") {
          $("#searchfield").attr('class', 'sercfield_menu red');
          $('#searchfield').focus();
          return false;
        } else if (!searchReg.test(val)) {
          alert('Oops! You might have entered the invalid keyword(s). Kindly re-enter.');
          $("#searchfield").attr('class', 'sercfield red');
          $('#searchfield').focus();
          return false;
        }
        val = $.trim(val);
        str = val.replace(/\s+/g, '-').toLowerCase();
        window.location.href = '/topic/' + str;
        return true;
      }
    });
});



(function ($) {
    $(document).ready(function () {
        $('#menu ul ul li:odd').addClass('odd');
        $('#menu ul ul li:even').addClass('even');
        $('#menu > ul > li > span.sub-menu').click(function () {
          $('#menu li').removeClass('active');
          $(this).closest('li').addClass('active');
          var checkElement = $(this).next();
          if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            $(this).closest('li').removeClass('active');
            checkElement.slideUp('normal');
          }
          if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $('#menu ul ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
          }
          if ($(this).closest('li').find('ul').children().length == 0) {
            return true;
          } else {
            return false;
          }
        });
    });
})(jQuery);
$('document').ready(function () {
  $('#vuukle-reminder').on("click", function () {
    sendGA('comment button', 'clicked', 'vuukle');
  });
  var lastScroll = 0;
  var startleftScroll = 0;
  var startrightScroll = 0;
  $('.inPics .scrollBar').scroll(function () {
    var st = $(this).scrollLeft();
    if (st > lastScroll && startleftScroll == 0) {
      startleftScroll = 1;
      sendGA('In Pics Scroll right', 'clicked', 'navigation');
    } else if (startrightScroll == 0) {
      startrightScroll = 1;
      sendGA('In Pics Scroll left', 'clicked', 'navigation');
    }
    lastScroll = st;
  });
  var lastScroll2 = 0;
  var startleftScroll2 = 0;
  var startrightScroll2 = 0;
  $('.videos .scrollBar').scroll(function () {
    var st = $(this).scrollLeft();
    if (st > lastScroll2 && startleftScroll2 == 0) {
      startleftScroll2 = 1;
      sendGA('Videos Scroll right', 'clicked', 'Videos Widgets');
    } else if (startrightScroll2 == 0) {
      startrightScroll2 = 1;
      sendGA('Videos Scroll left', 'clicked', 'Videos Widgets');
    }
    lastScroll2 = st;
  });
  $('.sub-menu').on('click', function () {
    if ($(this).parent().hasClass('has-sub active')) {
      sendGA('Click to expand category', 'clicked', 'Expand Category');
    } else {
      sendGA('Click to collapse category', 'clicked', 'Collapse Category');
    }
  });
  $('li.active a').focus();
  var a = 12;
    $(".clickfor-opt").on('click', function (e) {
      if (a == 12) {
        $(this).addClass("active");
        $(".l-opt-site").show();
        a = 0;
      } else {
        $(this).removeClass("active");
        $(".l-opt-site").hide()
        a = 12;
      }
    });
  $("#serch-btn").click(function () {
    if (!($(this).hasClass('active'))) {
      $(this).addClass('active');
      $(".searchBox").addClass('open-box');
      $('#searchfield').focus();
    } else {
      $(this).removeClass('active');
      $(".searchBox").removeClass('open-box');
    }
  });  
});
$('.forcomment').click(function () {
  $('html, body').animate({
    scrollTop: $('#vuukle_div').offset().top
  }, 500);
  return false;
});
$(".prodetails .read_more").click(function () {
  if ($(this).hasClass("active")) {
    $(this).removeClass("active");
    $(".read_more .text").text("read more");
    $(".prodetails .info").css("height", "64px");
  } else {
    $(this).addClass("active");
    $(".read_more .text").text("read less");
    $(".prodetails .info").css("height", "auto");
  }
});
$(".bolly-prodetails .read_more").click(function () {
  if ($(this).hasClass("active")) {
    $(this).removeClass("active");
    $(".read_more .text").text("read more");
    $(".bolly-prodetails .info").css("height", "64px");
  } else {
    $(this).addClass("active");
    $(".read_more .text").text("read less");
    $(".bolly-prodetails .info").css("height", "auto");
  }
});
$(function() {
    $(".morCate").click(function(){
            $('.morCatelist').slideToggle('fast')
    });
    $('body').click(function (e) {
      var elem = $(e.target).attr('class');
      if ((elem != 'childe')
                      && (elem != 'morCatelist')
                      && (elem != 'morCate pull-right')
                      && (elem != 'linkicon')
                      && (elem != 'dot')
                      && (elem != 'listname')
                      ) {
            //hide navigation please 
            $('.morCatelist').hide('fast')
      }
    });	
    
    if(navigator.userAgent.toLowerCase().match('android')) {
      var banner_link = 'https://play.google.com/store/apps/details?id=com.indiatv.livetv&referrer=utm_source%3DIndiaTVNews_site%26utm_medium%3DSticky_Top_Banner';
      var banner_text = 'Google Play';
      var contest_url = 'https://play.google.com/store/apps/details?id=com.indiatv.livetv&referrer=utm_source%3Dafter_story_banner_m%26utm_campaign%3Dafter_story_banner_m';
    } else {
      var banner_link = 'https://itunes.apple.com/app/apple-store/id964186148?pt=117693642&ct=IndiaTVNews_topbanner&mt=8';
      var banner_text = 'Apple Store';
      var contest_url = 'https://itunes.apple.com/app/apple-store/id964186148?pt=117693642&ct=after_story_banner_m&mt=8';
    }
    var banner_html = '<div class="app-top-ad"><img src="https://static.khabarindiatv.com/khabar-global/banner/app-logo.png" alt="App logo"><h4>IndiaTV News</h4><h5><span>indiatvnews.com</span> Free - In '+banner_text+'</h5><a href="'+banner_link+'" class="install-app animated pulse">Install</a><span class="app-ad-close" onclick=\'$(\".app-top-ad\").remove();$(\".fixd-menu-top\").addClass(\"fixd-menu\");setCookieCom(\"mobile_banner\",\"yes\",1);\'></span></div>';
    //if($('#div-gpt-ad-1522826211487-2').html()!=undefined && $('#div-gpt-ad-1522826211487-2').html()!='' && $('article > h1.article-title').html()!=undefined && $('article > h1.article-title').html()!='' ){
    // $('<div class="row mb10 mt10"><a href="https://docs.google.com/forms/d/e/1FAIpQLScKxsfm3sZI4X7UKHjUiTg47kd-jt1Bxfz3GaF_y4u0ma9gsA/viewform"><img src="https://static.khabarindiatv.com/khabar-global/banner/andhadhun300x35new.jpg"></a></div>').insertBefore($('#div-gpt-ad-1522826211487-2').parent('div.row'));
    //}
    if(getCookieCom('mobile_banner')=='yes') {
      /* nothing */
    }else {
      //$('body').prepend(banner_html);
      $(window).scroll(function (e) {
        if($(this).scrollTop()>0) {
          $(".fixd-menu-top").addClass('fixd-menu');
        } else {
          $(".fixd-menu-top").removeClass('fixd-menu');
        }
      });
    }
});
function setCookieCom(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays*24*60*60*1000));
	var expires = "expires="+d.toUTCString();
	document.cookie = cname + "=" + cvalue + "; " + expires +";path=/";
}
function getCookieCom(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1);
		if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
	}
	return "";
}
